Struct Vs Class When To Use Which · TL;DR

1 min read
Mid-level7 min read
Rapid overview

TL;DR

Featurestructclass
Type categoryValue typeReference type
Memory allocationStored inline (stack, or inside another object)Stored on heap, referenced via pointer
Default behaviorCopied by value (creates a full copy)Copied by reference (points to same object)
NullabilityCannot be null (unless Nullable<T>)Can be null
InheritanceCannot inherit from another struct or class; only from ValueTypeSupports inheritance and polymorphism
InterfacesCan implement interfacesCan implement interfaces and base classes
Default constructorCannot define a custom parameterless constructor (C# 10 adds limited support)Can freely define constructors
Finalizer / DestructorNot supportedSupported
GC behaviorUsually short-lived, reclaimed when out of scopeManaged by the Garbage Collector
Boxing / UnboxingConverting to/from object/interface causes allocationNo boxing/unboxing issues
Thread safetySafer for small immutable dataReference types require synchronization if shared

See also